package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.templates.DestinationTemplate;
import cn.wolfcode.luowowo.search.templates.StrategyTemplate;
import cn.wolfcode.luowowo.search.templates.TravelTemplate;
import cn.wolfcode.luowowo.search.templates.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;
    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        //type -1 全部 0 目的地 1 攻略 2 游记 3用户
        switch (qo.getType()){
            case 0:
                    return destSearch(model,qo);
            case 1:
            return strategySearch(model,qo);
            case 2:
            return travelSearch(model,qo);
            case 3:
            return userSearch(model,qo);
            default:
               return  allSearch(model,qo);

        }

    }

    private String allSearch(Model model, SearchQueryObject qo) {
        //目的地
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        //攻略
        Page<StrategyTemplate> strategy = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        SearchResultVO vo = new SearchResultVO();
        vo.setUsers(users.getContent());
        vo.setStrategys(strategy.getContent());
        vo.setTravels(travels.getContent());
        vo.setDests(dests.getContent());
        Long totals =  dests.getTotalElements() + strategy.getTotalElements()+travels.getTotalElements()+users.getTotalElements();
        vo.setTotal(totals);
        model.addAttribute("data",vo);
        return "index/searchAll";
    }

    private String userSearch(Model model, SearchQueryObject qo) {
        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",users);
        return "index/searchUser";
    }

    private String travelSearch(Model model, SearchQueryObject qo) {
        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",travels);
        return "index/searchTravel";
    }

    private String strategySearch(Model model, SearchQueryObject qo) {
        //攻略
        Page<StrategyTemplate> strategy = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",strategy);
        return "index/searchStrategy";
    }

    private String destSearch(Model model, SearchQueryObject qo) {
        //根据关键字查询目的地对象
       DestinationTemplate dest =  destinationSearchService.queryDestByName(qo.getKeyword());
        //如果存在,查询该目的地下的所有攻略,游记,用户
        if(dest != null){
            qo.setPageSize(5);
            qo.setDestId(dest.getId());
            //查询游记
           Page<TravelTemplate> travels = travelSearchService.query(qo);
           //查询攻略
            Page<StrategyTemplate> strategys = strategySearchService.queryByDestId(qo);
            //查询用户
            Page<UserInfoTemplate> userInfo = userInfoSearchService.query(dest.getName());
            //data

            SearchResultVO vo = new SearchResultVO();
            vo.setTravels(travels.getContent());
            vo.setStrategys(strategys.getContent());
            vo.setUsers(userInfo.getContent());




            long total = travels.getTotalElements() + strategys.getTotalElements() + userInfo.getTotalElements();
            vo.setTotal(total);
            model.addAttribute("data",vo);
            //qo
            //dest
            model.addAttribute("dest",dest);
        }
        return "index/searchDest";
    }
    @RequestMapping("/queryDest")
    public String queryDest(Model model,SearchQueryObject qo){
        List<DestinationTemplate> dest = destinationSearchService.questDestByKeyWord(qo);
        model.addAttribute("dest",dest);
        return "destination/destTpl";
    }
}
